﻿Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn"))
    Dim ids As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ids = Request.QueryString("id")
        Label2.Text = Session("username")
        Try

            If (ids >= 1) Then ' we started imageid from 1
                Dim cmd As New SqlCommand("select * from imagetable where imageid=" + ids, cn)
                Dim dr As SqlDataReader
                cn.Open()
                dr = cmd.ExecuteReader
                dr.Read()
                Image1.ImageUrl = dr("imageurl")
                Image1.Height = 500
                Image1.Width = 500
                Label4.Text = dr("likes")
                Label5.Text = dr("dislikes")
                cn.Close()
                cn.Open()


                Dim sql As String = "select Title,Description,UploadDate,UploadBy FROM IMAGETABLE WHERE IMAGEID=" + ids
                Dim sd1 As New SqlDataSource(cn.ConnectionString, sql)
                DetailsView2.DataSource = sd1
                DetailsView2.DataBind()
                cn.Close()
            End If

            Dim cmd1 As SqlCommand = New SqlCommand("select commentby,comment,cdate from comment where imageid=" + ids, cn)
            Dim sd2 As New SqlDataSource(cn.ConnectionString, cmd1.CommandText)
            GridView1.DataSource = sd2
            GridView1.DataBind()
        Catch ex As Exception

        End Try








    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        If Session("username") = "Guest" Then
            Label3.Text = "Login First"
        Else
            Try
                cn.Open()
                Dim cmd As New SqlCommand("update imagetable set likes=likes+1 where imageid=" + ids, cn)
                cmd.ExecuteNonQuery()
                cn.Close()
                Label3.Text = "Thank you for giving your opinion"
            Catch ex As Exception


            End Try
            
        End If
     
    End Sub

    Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
        If Session("username") = "Guest" Then
            Label3.Text = "Login First"
        Else
            Try
                cn.Open()
                Dim cmd As New SqlCommand("update imagetable set dislikes=dislikes+1 where imageid=" + Request.QueryString("id"), cn)
                cmd.ExecuteNonQuery()
                cn.Close()
                Label3.Text = "Thank you for giving your opinion"
            Catch ex As Exception


            End Try

        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("add comment.aspx?id=" + ids)

    End Sub


    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Response.Redirect("chart.aspx?id=" + Request.QueryString("id"))
    End Sub
End Class
